<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        round
        type="info"
        size="small"
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 频道列表 -->
    <!-- 通过 v-model 绑定当前激活标签对应的索引值，默认情况下启用第一个标签。 -->
    <!-- 通过 animated 属性可以开启切换标签内容时的转场动画 -->
    <!-- 通过 swipeable 属性可以开启滑动切换标签页 -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab
        v-for="item in userChannelsList"
        :key="item.id"
        :title="item.name"
      >
        <!-- 文章列表 :channel="item"传参-->
        <article-list :channel="item" />
      </van-tab>
      <div
        slot="nav-right"
        class="hamburger-btn"
        @click="isChannelEditShow = true"
      >
        <i class="iconfont icon-gengduo"></i>
      </div>
    </van-tabs>
    <!-- /频道列表 -->

    <!-- 频道编辑弹出层 -->
    <van-popup
      v-model="isChannelEditShow"
      closeable
      position="bottom"
      close-icon-position="top-left"
      :style="{ height: '100%' }"
    >
      <!-- 组件传参 :myChannels="userChannelsList" active 控制频道高亮-->
      <channel-Edit
        :myChannels="userChannelsList"
        :active="active"
        @update-active="onUpadateActive"
      ></channel-Edit>
    </van-popup>
    <!-- /频道编辑弹出层 -->
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user.js'
import ArticleList from '@/views/home/component/article-list.vue'
import ChannelEdit from '@/views/home/component/channel-edit.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage.js'

export default {
  name: 'HomeIndex',
  components: {
    ArticleList, //注册组件
    ChannelEdit,
  },
  data() {
    return {
      active: 0,
      // 用户频道列表
      userChannelsList: [],
      isChannelEditShow: false, //控制频道编辑弹出层的隐藏与显示
    }
  },
  computed: {
    ...mapState(['user']),
  },
  mounted() {},
  created() {
    this.loadChannels()
  },
  methods: {
    // 加载用户频道列表函数
    async loadChannels() {
      try {
        // const { data } = await getUserChannels()
        // // console.log(data.data.channels);
        // this.userChannelsList = data.data.channels
        let channels = []
        if (this.user) {
          //已登录，请求获取用户频道列表
          const { data } = await getUserChannels()
          channels = data.data.channels
        } else {
          // 未登录，判断是否有本地的频道列表数据
          const localChannels = getItem('TOUTIAO_CHANNELS')
          // 有,拿来使用
          if (localChannels) {
            channels = localChannels
          } else {
            // 没有，请求获取默认频道列表
            const { data } = await getUserChannels()
            channels = data.data.channels
          }
        }
        this.userChannelsList = channels
      } catch (err) {
        this.$toast('获取频道列表失败')
      }
    },
    // 子组件使用this.$emit()向父组件传 实现在我的频道点击跳转到某频道
    onUpadateActive(index, isChannelEditShow = true) {
      this.active = index
      //接着关闭弹层
      // es6新宇语法 不传参 为默认值  传参 则为新值
      this.isChannelEditShow = isChannelEditShow
    },
  },
}
</script>

<style lang="less" scoped>
.home-container {
  padding-top: 174px;
  padding-bottom: 100px;
  :deep(.van-nav-bar__title) {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
      color: aliceblue;
    }
  }
  :deep(.channel-tabs) {
    .van-tabs__wrap {
      height: 82px;
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
    }
    .van-tab {
      min-width: 200px;
      border-right: 1px solid #edeff3;
      font-size: 30px;
      color: #777777;
    }
    // 点击标签激活时才有该类名
    .van-tab--active {
      color: #333333;
    }
    .van-tabs__nav {
      padding-bottom: 0;
    }
    // 底部条
    .van-tabs__line {
      width: 31px;
      height: 6px;
      background-color: #3296fa;
      bottom: 8px;
    }
  }
  // 汉堡按钮 固定定位
  .hamburger-btn {
    position: fixed;
    right: 0;
    width: 66px;
    height: 82px;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #fff;
    opacity: 0.902;
    i.iconfont {
      font-size: 33px;
    }
    &:before {
      content: '';
      width: 1px;
      height: 100%;
      background-image: url('~@/assets/gradient-gray-line.png');
      position: absolute;
      left: 0;
      background-size: contain; //让长边显示出来
    }
  }
}
</style>
